//
//  newsestDetailWebViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "newsestDetailWebViewController.h"

@interface newsestDetailWebViewController ()<UIWebViewDelegate,UMSocialUIDelegate>

@property(nonatomic, strong)UIActivityIndicatorView  *activityIndicator;
@property(nonatomic, strong)UIView *myView;
@end

@implementation newsestDetailWebViewController

- (void)dealloc
{
    [_activityIndicator release];
    [_myView release];
    [super dealloc];
}

//- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    //加载H5网页
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    webView.scalesPageToFit = YES;
    webView.delegate = self;
    [webView setOpaque:NO];//opaque是不透明的意思
    [webView setScalesPageToFit:YES];//自动缩放以适应屏幕

   NSLog(@"%@",self.type);
    if ([self.type isEqualToString:@"3"]) {
        NSLog(@"%@",self.ID);
        NSString *string = [NSString stringWithFormat:@"http://v.autohome.com.cn/v_4_%@.html",self.ID];
        NSURL *url = [NSURL URLWithString:string];
        NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
        [webView loadRequest:request];//加载
    }else{
        NSString *string = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.2.5/content/News/newscontent-a2-pm1-v4.2.5-n%@-lz0-sp0-nt0-sa1-p0-c1-fs0-cw320.html",self.ID];
        NSURL *url = [NSURL URLWithString:string];
        NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
        [webView loadRequest:request];//加载
    }
    [self.view addSubview:webView];
    [webView release];
    
    
    //左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    //右按钮们
    UIBarButtonItem *itemOne = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"np_like_h"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(itemOneAction:)];
    UIBarButtonItem *itemTwo = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(itemTwoAction:)];
    self.navigationItem.rightBarButtonItems = @[itemTwo, itemOne];
    
}


//正在加载
-(void)webViewDidStartLoad:(UIWebView *)webView{
    if ([self.type isEqualToString:@"3"]) {
        
    }else{
    //创建UIActivityIndicatorView背底半透明View
    self.myView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.myView setBackgroundColor:[UIColor blackColor]];
    [self.myView setAlpha:0.5];
    [self.view addSubview:self.myView];
    [_myView release];
    
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:self.view.bounds];
    [self.activityIndicator setCenter:self.myView.center];
    [self.activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [self.view addSubview:self.activityIndicator];
    [self.activityIndicator startAnimating];
    [_activityIndicator release];
    }
}


//html加载完后触发
-(void)webViewDidFinishLoad:(UIWebView*)webView{
    [self.activityIndicator stopAnimating];
    [self.myView removeFromSuperview];
    //
    NSString *js= @"var script = document.createElement('script');"
    "script.type = 'text/javascript';"
    "script.text = \"function ResizeImages() { "
    "var myimg,oldwidth;"
    "var maxwidth = %f;"
    "for(i=0;i <document.images.length;i++){"
    "myimg = document.images[i];"
    "if(myimg.width > maxwidth){"
    "oldwidth = myimg.width;"
    "myimg.width = %f;"
    "}"
    "}"
    "}\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    js=[NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width-15];
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    //修改字体大小
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '110%'"];
    //背景颜色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFFFF'"];
    //字体颜色
   // [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
}
//当请求页面出现错误的时候，我们给予提示
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    UIAlertController *alart = [UIAlertController alertControllerWithTitle:@"" message:@"网络不给力😂" preferredStyle:UIAlertControllerStyleAlert];
   [self presentViewController:alart animated:YES completion:nil];
    //自动消失的思路就是添加一个计时器,设置计时器的时间,让UIAlertController 通过dismiss消失
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(creatAlert:) userInfo:alart repeats:NO];
}
//定时器方法
- (void)creatAlert:(NSTimer *)timer{
    UIAlertController *alert = [timer userInfo];
    [alert dismissViewControllerAnimated:YES completion:nil];
    alert = nil;
}







-(void)itemTwoAction:(UIBarButtonItem *)item{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"wx4e800b840d82e365" shareText:[NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.2.5/content/News/newscontent-a2-pm1-v4.2.5-n%@-lz0-sp0-nt0-sa1-p0-c1-fs0-cw320.html",self.ID] shareImage:[UIImage imageNamed:@"icon-76.png"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite, nil] delegate:self];
    
}
-(void)itemOneAction:(UIBarButtonItem *)item{
    NSLog(@"12");

}




-(void)leftAction{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
